/**
 * \file scalar.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_SCALAR_HPP_INCLUDED
#define BASIS_SCALAR_HPP_INCLUDED

#include <basis/detail/config.hpp>

namespace basis
{
	//------------------------------------------------------------
	// Vector forward declarations
	//------------------------------------------------------------

	template <typename Real, typename Rep>
	class vector2;

	template <typename Real, typename Rep>
	class vector3;

	template <typename Real, typename Rep>
	class vector4;

	/**
	 * A vector type where all the values are the same.
	 *
	 * \author Don Olmstead
	 * \version 0.1
	 */
	template <typename Real, typename Rep>
	class scalar
	{
		public:

			//------------------------------------------------------------
			// Member types
			//------------------------------------------------------------

			/// A real number type
			typedef Real value_type;
			/// Reference to a real number type
			typedef Real& reference;
			/// Unsigned integral type
			typedef std::size_t size_type;
			/// The internal storage type
			typedef Rep simd_type;
			/// A 4 component vector
			typedef typename vector4<Real, Rep> vector4_type;

			//------------------------------------------------------------
			// Construction/Destruction
			//------------------------------------------------------------

			/**
			 * Creates an instance of the scalar class.
			 *
			 * Left uninitialized to speed construction.
			 */
			inline scalar()
			{ }

			/**
			 * Initializes an instance of the scalar class.
			 *
			 * \param value The scalar value.
			 */
			inline scalar(const value_type value)
				: _rep(value)
			{ }

			/**
			 * Initializes an instance of the scalar class.
			 *
			 * \param copy The instance to copy.
			 */
			inline scalar(const scalar& copy)
				: _rep(copy._rep)
			{ }

			//------------------------------------------------------------
			// Assignment
			//------------------------------------------------------------

			/**
			 * Assignment operator for the vector4 class.
			 *
			 * \param copy The instance to copy.
			 */
			inline scalar& operator= (const scalar& copy)
			{
				_rep = copy._rep;

				return *this;
			}

			/**
			 * Accessor for the size of the scalar.
			 *
			 * Size is the number of elements that can be accessed
			 * within the type.
			 *
			 * \returns The size of the vector4.
			 */
			inline static size_type size()
			{
				return 1;
			}

			/**
			 * Accessor for the capacity of the scalar.
			 *
			 * Capacity is the number of elements that could be
			 * stored in the type.
			 *
			 * \returns The capacity of the vector4.
			 */
			inline static size_type capacity()
			{
				return Rep::capacity();
			}

			//------------------------------------------------------------
			// Conversion
			//------------------------------------------------------------

			/**
			 * Retrieves the value held in the scalar.
			 *
			 * \returns The value held in the scalar.
			 */
			inline operator value_type() const
			{
				return _rep[0];
			}

			//------------------------------------------------------------
			// Comparison operators
			//------------------------------------------------------------

			/**
			 * Tests scalars for equality.
			 *
			 * \param rhs Source vector.
			 * \returns \b true if the scalars are equal; \b false otherwise.
			 */
			inline bool operator== (const scalar& rhs) const
			{
				return Rep::equal(_rep, rhs._rep) == Rep::mask_all();
			}

			/**
			 * Tests scalars for inequality.
			 *
			 * \param rhs Source vector.
			 * \returns \b true if the scalars are not equal; \b false otherwise.
			 */
			inline bool operator!= (const scalar& rhs) const
			{
				return Rep::not_equal(_rep, rhs._rep) == Rep::mask_all();
			}

			/**
			 * Tests whether one scalar is less than another scalar.
			 *
			 * \param rhs Source value.
			 * \returns \b true if the left side is less than the right side; \b false otherwise.
			 */
			inline bool operator< (const scalar& rhs) const
			{
				return Rep::less_than(_rep, rhs._rep) == Rep::mask_all();
			}

			/**
			 * Tests whether one scalar is less than or equal to another scalar.
			 *
			 * \param rhs Source value.
			 * \returns \b true if the left side is less than or equal to the right side; \b false otherwise.
			 */
			inline bool operator<= (const scalar& rhs) const
			{
				return Rep::less_than_equal(_rep, rhs._rep) == Rep::mask_all();
			}

			/**
			 * Tests whether one scalar is greater than another scalar.
			 *
			 * \param rhs Source value.
			 * \returns \b true if the left side is greater than the right side; \b false otherwise.
			 */
			inline bool operator> (const scalar& rhs) const
			{
				return Rep::greater_than(_rep, rhs._rep) == Rep::mask_all();
			}

			/**
			 * Tests whether one scalar is greater than or equal to another scalar.
			 *
			 * \param rhs Source value.
			 * \returns \b true if the left side is greater than or equal to the right side; \b false otherwise.
			 */
			inline bool operator>= (const scalar& rhs) const
			{
				return Rep::greater_than_equal(_rep, rhs._rep) == Rep::mask_all();
			}				

			//------------------------------------------------------------
			// Arithmetic operators
			//------------------------------------------------------------

			/**
			 * Negates the scalar.
			 *
			 * \returns The negation of the scalar.
			 */
			inline const scalar operator- ()
			{
				return scalar(-_rep);
			}

			/**
			 * Adds two scalars.
			 *
			 * \param rhs Source value.
			 * \returns Result of the addition.
			 */
			inline const scalar operator+ (const scalar& rhs) const
			{
				return scalar(_rep + rhs._rep);
			}

			/**
			 * Subtracts a scalar from a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the subtraction.
			 */
			inline const scalar operator- (const scalar& rhs) const
			{
				return scalar(_rep - rhs._rep);
			}

			/** 
			 * Multiplies a scalar by a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the multiplication.
			 */
			inline const scalar operator* (const scalar& rhs) const
			{
				return scalar(_rep * rhs._rep);
			}

			/**
			 * Divides a scalar by a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the division.
			 */
			inline const scalar operator/ (const scalar& rhs) const
			{
				return scalar(_rep / rhs._rep);
			}

			/**
			 * Adds two scalars.
			 *
			 * \param rhs Source value.
			 * \returns Result of the addition.
			 */
			inline scalar& operator+= (const scalar& rhs)
			{
				_rep += rhs._rep;

				return *this;
			}

			/**
			 * Subtracts a scalar from a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the subtraction.
			 */
			inline scalar& operator-= (const scalar& rhs)
			{
				_rep -= rhs._rep;

				return *this;
			}

			/** 
			 * Multiplies a scalar by a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the multiplication.
			 */
			inline scalar& operator*= (const scalar& rhs)
			{
				_rep *= rhs._rep;

				return *this;
			}

			/**
			 * Divides a scalar by a scalar.
			 *
			 * \param rhs Source value.
			 * \returns Result of the division.
			 */
			inline scalar& operator/= (const scalar& rhs)
			{
				_rep /= rhs._rep;

				return *this;
			}

			/**
			 * Multiplies a scalar by a vector.
			 *
			 * \param rhs Source value.
			 * \returns Result of the multiplication.
			 */
			inline const vector4_type operator* (const vector4_type& rhs) const
			{
				return vector4_type(_rep * rhs._rep);
			}

			/**
			 * Divides a scalar by a vector.
			 *
			 * \param rhs Source value.
			 * \returns Result of the division.
			 */
			inline const vector4_type operator/ (const vector4_type& rhs) const
			{
				return vector4_type(_rep / rhs._rep);
			}

			//------------------------------------------------------------
			// Arithmetic operations
			//------------------------------------------------------------

			/**
			 * Takes the inverse of a scalar.
			 *
			 * \param value Source value.
			 * \returns The inverse of a scalar.
			 */
			inline static const scalar inv(const scalar& value)
			{
				return scalar(Rep::inv(value._rep));
			}

			/**
			 * Takes the square root of a scalar.
			 *
			 * \param value Source value.
			 * \returns The square root of a scalar.
			 */
			inline static const scalar sqrt(const scalar& value)
			{
				return scalar(Rep::sqrt(value._rep));
			}

			/**
			 * Takes the inverse square root of a scalar.
			 *
			 * \param value Source value.
			 * \returns The inverse square root of a scalar.
			 */
			inline static const scalar inv_sqrt(const scalar& value)
			{
				return scalar(Rep::inv_sqrt(value._rep));
			}

			/**
			 * Returns the lowest value.
			 *
			 * \param value1 Source value.
			 * \param value2 Source value.
			 * \returns The lowest value.
			 */
			inline static const scalar min(const scalar& value1, const scalar& value2)
			{
				return scalar(Rep::min(value1._rep, value2._rep));
			}

			/**
			 * Returns the highest value.
			 *
			 * \param value1 Source value.
			 * \param value2 Source value.
			 * \returns The lowest value.
			 */
			inline static const scalar max(const scalar& value1, const scalar& value2)
			{
				return scalar(Rep::max(value1._rep, value2._rep));
			}

			/**
			 * Restricts a value to be within a specified range.
			 *
			 * \param value Source value.
			 * \param minimum The minimum value.
			 * \param maximum The maximum value.
			 * \returns The clamped value.
			 */
			inline static const scalar clamp(const scalar& value, const scalar& minimum, const scalar& maximum)
			{
				return scalar(Rep::max(minimum._rep, Rep::min(maximum._rep, value._rep)));
			}

		private:
			
			friend class vector2<Real, Rep>;
			friend class vector3<Real, Rep>;
			friend class vector4<Real, Rep>;

			/**
			 * Initializes an instance of the scalar class.
			 *
			 * \param rep Internal representation to use.
			 */
			inline scalar(const simd_type& rep)
				: _rep(rep)
			{ }

			/// Internal representation of the type
			simd_type _rep;

	} ; // end class scalar

} // end namespace basis

#endif // end BASIS_SCALAR_HPP_INCLUDED
